package itsm.isperp.module.controller.app;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.utils.TreeNodeUtils;
import itsm.isperp.framework.web.controller.TreeNodeController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.app.AppMenu;
import itsm.isperp.module.entity.app.AppUploadFile;
import itsm.isperp.module.entity.app.AppUserCustomStyle;
import itsm.isperp.module.entity.app.AppUserWebosMenu;
import itsm.isperp.module.service.app.AppMenuService;
import itsm.isperp.module.service.app.AppUploadFileService;
import itsm.isperp.module.service.app.AppUserCustomStyleService;
import itsm.isperp.module.service.app.AppUserWebosMenuService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 系统菜单控制器
 * 
 * @author lizx
 * @date 2013-1-12
 * @version V1.0
 */
@Controller
@RequestMapping("/app/menu")
public class AppMenuController extends TreeNodeController<AppMenu> {

	/**
	 * 系统菜单管理服务
	 */
	@Autowired
	protected AppMenuService appMenuService;

	@Autowired
	protected AppUploadFileService appUploadFileService;

	@Override
	public AppMenuService getService() {
		return this.appMenuService;
	}

	@Override
	public String getPageTitle() {
		return "系统菜单";
	}

	@RequestMapping("save")
	@ResponseBody
	public ResultMessage save(AppMenu model, HttpServletRequest req) {

		AppUploadFile file = appUploadFileService.getOneBySourceId(
				model.getId(), "id", "path");

		if (file != null) {
			model.setIconImage(file.getPath());
			appUploadFileService.deleteBySourceId(model.getId());
		}

		this.getService().saveOrUpdate(model, req);

		ContextHolder.reloadAuthorityResourceDefinition();
		return ResultMessage.success();
	}

	@ResponseBody
	@RequestMapping(value = "getModuleMenu")
	public List<? extends ITreeNode> getModuleMenu(String moduleId) {

		// 获取人员的菜单
		List<AppMenu> children = this.appMenuService
				.findUsernameMenuByParentId(moduleId);

		return TreeNodeUtils.parseFormList(children);
	}

	@ResponseBody
	@RequestMapping(value = "getWebosMenu")
	public List<Map<String, Object>> getWebosMenu() {

		return this.appMenuService.getWebosMenu();

	}

	@RequestMapping(value = "webosSettingEdit")
	public ModelAndView webosSettingEdit() {
		// 获取用户所有的webos图标

		List<Map<String, Object>> resultMeuns = this.appMenuService
				.getWebosMenu();

		List<Map<String, Object>> screen1 = new ArrayList<>();
		List<Map<String, Object>> screen2 = new ArrayList<>();
		List<Map<String, Object>> screen3 = new ArrayList<>();

		for (Map<String, Object> m : resultMeuns) {
			Object indexNum = m.get("index_num");
			if (indexNum == null) {

				screen1.add(m);
			} else {
				int i = Integer.parseInt(indexNum.toString());
				switch (i) {
				case 1:
					screen1.add(m);
					break;
				case 2:
					screen2.add(m);
					break;
				case 3:
					screen3.add(m);
					break;
				default:
					screen1.add(m);
					break;
				}
			}

		}
		ModelAndView mv = this.getModelAndView();

		mv.addObject("screen1", screen1);
		mv.addObject("screen2", screen2);
		mv.addObject("screen3", screen3);

		return mv;

	}

	@RequestMapping(value = "webosSetting/save")
	@ResponseBody
	public ResultMessage webosSettingSave(HttpServletRequest request) {

		String[] menuIds = request.getParameterValues("menuIds");
		String[] indexNums = request.getParameterValues("indexNums");

		AppUserWebosMenuService appUserWebosMenuService = ContextHolder
				.getSpringBean("appUserWebosMenuService");
		String username = ContextHolder.getLoginUsername();
		appUserWebosMenuService.deleteByUsername(username);

		List<AppUserWebosMenu> ms = new ArrayList<>();
		for (int i = 0; i < menuIds.length; i++) {

			AppUserWebosMenu m = new AppUserWebosMenu();
			m.setId(HibernateUUIDGenerator.generate());
			m.setOrderNum(i + 1);
			m.setAppMenuId(menuIds[i]);
			m.setIndexNum(Integer.parseInt(indexNums[i]));
			ms.add(m);
		}
		if (ms.size() > 0)
			appUserWebosMenuService.saveBatch(username, ms);

		String styleKey = request.getParameter("styleKey");
		if (StringUtils.isNotEmpty(styleKey)) {

			AppUserCustomStyleService appUserCustomStyleService = ContextHolder
					.getSpringBean("appUserCustomStyleService");
			AppUserCustomStyle cs = appUserCustomStyleService
					.findByUsername(username);
			if (cs == null) {
				cs = new AppUserCustomStyle();
				cs.setId(HibernateUUIDGenerator.generate());
				cs.setAppUserName(username);

			}
			cs.setStyleKey(styleKey);

			appUserCustomStyleService.saveOrUpdate(cs);

		}

		ContextHolder.reloadAuthorityResourceDefinition();

		return ResultMessage.success();
	}
	@RequestMapping(value = "getCurUserRight")
	@ResponseBody
	public boolean getCurUserRight(HttpServletRequest request) {
		String tUrl=request.getParameter("turl");
		return this.getService().getCurUserRight(tUrl);
		
	}
}
